package com.jingdianjichi.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.common.entity.PageResponse;
import com.jingdianjichi.subject.common.enums.IsDeletdFlageEnum;
import com.jingdianjichi.subject.domain.convert.SubjectCategoryConverter;
import com.jingdianjichi.subject.domain.convert.SubjectInfoConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.handler.subject.SubjectTypeHandler;
import com.jingdianjichi.subject.domain.handler.subject.SubjectTypeHandlerFactory;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfo;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectInfoService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * SubjectCategoryDomainServiceImpl 类
 *
 * @author 熊苏鹏1
 * @date 2025/1/30 17:12
 * @description 无
 */
@Service
@Slf4j
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {

    @Resource
    SubjectLabelService subjectLabelService;
    @Resource
    SubjectTypeHandlerFactory subjectTypeHandlerFactory;
    @Resource
    private SubjectInfoService subjectInfoService;
    @Resource
    private SubjectMappingService subjectMappingService;

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        if(log.isInfoEnabled())
            log.info("SubjectInfoDomainServiceImpl add bo:{}", JSON.toJSONString(subjectInfoBO));
        //先更新题目表
        SubjectInfo subjectInfo = SubjectInfoConverter.INSTANCE.convertBoToInfo(subjectInfoBO);
        subjectInfo.setIsDeleted(IsDeletdFlageEnum.NOT_DELETED.getCode());
        subjectInfoService.insert(subjectInfo);
        subjectInfoBO.setId(subjectInfo.getId());
        //依据类型，策略工参模式选择
        SubjectTypeHandler subjectTypeHandler = subjectTypeHandlerFactory.getSubjectTypeHandler(subjectInfoBO.getSubjectType());
        subjectTypeHandler.add(subjectInfoBO);
        //将映射表也处理一下
        List<Integer> categoryIds = subjectInfoBO.getCategoryIds();
        List<Integer> labelIds = subjectInfoBO.getLabelIds();
        List<SubjectMapping> subjectMappings = new ArrayList<>();
        categoryIds.forEach(categoryId -> {
            labelIds.forEach(labelId -> {
                SubjectMapping subjectMapping = new SubjectMapping();
                subjectMapping.setCategoryId(Long.valueOf(categoryId));
                subjectMapping.setLabelId(Long.valueOf(labelId));
                subjectMapping.setSubjectId(subjectInfo.getId());
                subjectMapping.setIsDeleted(IsDeletdFlageEnum.NOT_DELETED.getCode());
                subjectMappings.add(subjectMapping);

            });
        });
        subjectMappingService.insertBatch(subjectMappings);

    }

    @Override
    public PageResponse<SubjectInfoBO> queryCountByCondition(SubjectInfoBO subjectInfoBO) {
        if(log.isInfoEnabled())
            log.info("SubjectInfoDomainServiceImpl queryByCondition bo:{}", JSON.toJSONString(subjectInfoBO));
        PageResponse<SubjectInfoBO> pageResponse = new PageResponse();
        pageResponse.setPageNum(subjectInfoBO.getPageNum());
        pageResponse.setPageSize(subjectInfoBO.getPageSize());
        SubjectInfo subjectInfo = SubjectInfoConverter.INSTANCE.convertBoToInfo(subjectInfoBO);
        int total = subjectInfoService.queryCount(subjectInfo,subjectInfoBO.getCategoryId(),subjectInfoBO.getLabelId());
        int start = pageResponse.getStart();
        List<SubjectInfo> subjectInfos = subjectInfoService.queryCountByCondition(subjectInfo,
                subjectInfoBO.getCategoryId(),subjectInfoBO.getLabelId(),start,subjectInfoBO.getPageSize());
        List<SubjectInfoBO> subjectInfoList = SubjectInfoConverter.INSTANCE.convertInfoListToBoList(subjectInfos);
        pageResponse.setResult(subjectInfoList);
        pageResponse.setTotal(total);
        return pageResponse;
    }

    @Override
    public SubjectInfoBO querySubjectDetail(SubjectInfoBO subjectInfoBO) {
        if(log.isInfoEnabled())
            log.info("SubjectInfoDomainServiceImpl querySubjectDetail bo:{}", JSON.toJSONString(subjectInfoBO));
        //先搜出题目的大表
        SubjectInfo subjectInfo = subjectInfoService.queryById(subjectInfoBO.getId());
        //通过工厂动态加载搜索方法
        SubjectTypeHandler subjectTypeHandler = subjectTypeHandlerFactory.getSubjectTypeHandler(subjectInfo.getSubjectType());
        SubjectAnswerBO query = subjectTypeHandler.query(subjectInfoBO);
        SubjectInfoBO subjectInfoBO1 = SubjectInfoConverter.INSTANCE.convertEntityAndBoToBo(subjectInfo, query);
        List<String> labelNames =        subjectMappingService.queryLabelName(subjectInfoBO.getId());
        //通过subjectId查mapping表去重，之后通过label表获取labelName
        subjectInfoBO1.setLabelNames(labelNames);
        return subjectInfoBO1;


    }
}
